<template>
	<view class="container" :class="themeStyle">
		<view class="content">
			<view class="wx-code">
				<image :src="$util.img(path)" />
			</view>
			<view class="get-coupon">
				<text class="coupon-name">{{ info.coupon_name }}</text>
				<view class="ns-text-color coupon-price uni-bold">￥{{ info.money }}</view>
				<text v-if="info.at_least > 0" class="ns-text-color-gray">满{{ info.at_least }}元使用</text>
				<text v-else class="ns-text-color-gray">无门槛优惠券</text>
				<view v-if="info.validity_type == 0" class="ns-text-color-gray">有效期至 {{ $util.timeStampTurnTime(info.end_time) }}</view>
				<view v-else class="ns-text-color-gray">领取之日起{{ info.fixed_term }}天内有效</view>

				<view class="get-span" @click="receiveGoodsCoupon(info.coupon_type_id)">
					<button type="primary" v-if="isCanReceive">{{ button }}</button>
					<button type="primary" disabled v-if="!isCanReceive">{{ button }}</button>
				</view>

				<view class="ns-text-color-gray ns-font-size-sm tips">注:扫描二维码或点击右上角进行分享</view>
			</view>
		</view>
		<loading-cover ref="loadingCover"></loading-cover>
		<ns-login ref="login"></ns-login>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				info: {},
				couponTypeId: 0,
				path: '',
				button: '领取',
				uid: 0,
				couponBtnSwitch: false, //获取优惠券防止重复提交
				memberId: 0,
				receivedNum: 0
			};
		},
		onLoad(data) {
			if (data.source_member) uni.setStorageSync('source_member', data.source_member);
			if (data.coupon_type_id) this.couponTypeId = data.coupon_type_id;

			// 小程序扫码进入
			if (data.scene) {
				var sceneParams = decodeURIComponent(data.scene);
				sceneParams = sceneParams.split('&');
				if (sceneParams.length) {
					sceneParams.forEach(item => {
						if (item.indexOf('coupon_type_id') != -1) this.couponTypeId = item.split('-')[1];
						if (item.indexOf('source_member') != -1) this.source_member = item.split('-')[1];
					});
				}
			}
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
			this.getCoupon();

			this.receivedNum = 0;

			if (uni.getStorageSync('token')) {
				this.getMemberId();
				this.getReceivedNum();
			}
		},
		computed: {
			//vueX页面主题
			themeStyle() {
				return 'theme-' + this.$store.state.themeStyle
			}
		},
		onHide() {
			this.couponBtnSwitch = false;
		},
		methods: {
			getCoupon() {
				this.$api.sendRequest({
					url: '/coupon/api/coupon/typeinfo',
					data: {
						coupon_type_id: this.couponTypeId
					},
					success: res => {
						let data = res.data;
						if (data) {
							this.info = data;
							this.path = data.qrcode;
							if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
							this.buttonRefresh();
						} else {
							this.$util.showToast({
								title: res.message,
								success: () => {
									setTimeout(() => {
										uni.navigateBack({
											delta: 1
										});
									}, 1000);
								}
							});
						}
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			receiveGoodsCoupon(couponTypeId) {
				if (!uni.getStorageSync('token')) {
					this.$refs.login.open('/otherpages/goods/coupon_receive/coupon_receive?coupon_type_id=' + this.couponTypeId);
					return;
				}

				if (!this.isCanReceive) return;

				if (this.couponBtnSwitch) return;
				this.couponBtnSwitch = true;

				this.$api.sendRequest({
					url: '/coupon/api/coupon/receive',
					data: {
						coupon_type_id: couponTypeId,
						site_id: this.info.site_id,
						get_type: 2 //获取方式:1订单2.直接领取3.活动领取
					},
					success: res => {
						if(res.code==-1&&res.error_code=='TOKEN_ERROR'){
							this.$util.showToast({'title':'登录失败'});
							let pageRouter=getCurrentPages();
							let datapage=pageRouter[pageRouter.length-1].options;
							let back=pageRouter[pageRouter.length-1].route;
							datapage.back=back;
							setTimeout(()=>{
								this.$util.redirectTo('/pages/login/login/login', datapage);
							},1500)
							return;
						}
						var data = res.data;
						let msg = res.message;
						if (res.code == 0) {
							msg = '领取成功';
							this.receivedNum += 1;
							this.buttonRefresh();
						}
						this.$util.showToast({
							title: msg
						});
						this.couponBtnSwitch = false;
					},
					fail: res => {
						this.couponBtnSwitch = false;
					}
				});
			},
			getMemberId() {
				this.$api.sendRequest({
					url: '/api/member/id',
					success: res => {
						if(res.code==-1&&res.error_code=='TOKEN_ERROR'){
							this.$util.showToast({'title':'登录失败'});
							let pageRouter=getCurrentPages();
							let data=pageRouter[pageRouter.length-1].options;
							let back=pageRouter[pageRouter.length-1].route;
							data.back=back;
							setTimeout(()=>{
								this.$util.redirectTo('/pages/login/login/login', data);
							},1500)
							return;
						}
						if (res.code >= 0) {
							this.memberId = res.data;
						}
					}
				});
			},
			getReceivedNum() {
				this.$api.sendRequest({
					url: '/coupon/api/coupon/receivedNum',
					data: {
						coupon_type_id: this.couponTypeId
					},
					success: res => {
						if (res.code >= 0) {
							this.receivedNum = res.data;
							this.buttonRefresh();
						}
					}
				});
			},
			buttonRefresh() {
				this.button = '领取';
				if (this.info.count == this.info.lead_count) {
					this.button = '来迟了该优惠券已被领取完了';
				} else if (this.info.max_fetch == 0 || this.receivedNum == 0) {
					this.button = '领取';
				} else if (this.info.max_fetch > this.receivedNum) {
					this.button = '领取';
				} else {
					this.button = '该优惠券领取已达到上限';
				}
			}
		},
		computed: {
			isCanReceive: {
				get() {
					if (this.info.count == this.info.lead_count) {
						return false;
					} else if (this.info.max_fetch == 0 || this.receivedNum == 0) {
						return true;
					} else if (this.info.max_fetch > this.receivedNum) {
						return true;
					} else {
						return false;
					}
				}
			}
		},
		/**
		 * 自定义分享内容
		 * @param {Object} res
		 */
		onShareAppMessage(res) {
			var title = '恭喜您获得一张' + this.info.money + '元的优惠券';
			if (this.info.range_type == 1) {
				title += `,满${this.info.at_least}全场可用`;
			} else {
				title += `,满${this.info.at_least}部分商品可用`;
			}
			var path = '/otherpages/goods/coupon_receive/coupon_receive?coupon_type_id=' + this.couponTypeId;
			if (this.memberId) path += '&source_member=' + this.memberId;
			return {
				title: title,
				// imageUrl: this.$util.img(this.goodsDetail.img_list[0].pic_cover_big),
				path: path,
				success: res => {},
				fail: res => {}
			};
		}
	};
</script>

<style lang="scss">
	// [data-theme] {
		page {
			height: 100%;
		}

		.container {
			background: #fff;
			padding: 0;
			height: 100%;
		}

		.content {
			background: url() no-repeat;
			background-size: 100% auto;
			height: 100%;
		}

		.wx-code {
			position: relative;
			margin: 0 auto;
			top: 34%;
			width: 320rpx;
			height: 320rpx;
			text-align: center;
			padding: 14rpx;
			box-shadow: 0 0 40rpx -2rpx;
		}

		.wx-code image {
			width: 320rpx;
			height: 320rpx;
		}

		.get-coupon {
			position: relative;
			margin: 0 auto;
			width: 560rpx;
			top: 37%;
			text-align: center;
			height: 366rpx;

			.coupon-name {
				font-size: $ns-font-size-lg;
			}
		}

		.get-span {
			text-align: center;
			display: block;
			border-radius: 6rpx;
			margin-top: 29px;
			margin-bottom: 20rpx;
			height: 80rpx;
			line-height: 80rpx;
			color: #fff;
		}

		.get-coupon .tips {
			height: 40rpx;
		}

		.coupon-price {
			font-size: 52rpx;
			height: 76rpx;
		}

		button {
			box-shadow: 1px 2px 5px rgba(0, 0, 0, 0.3);
		}
	// }
</style>
